package com.guigu.ababa.ababaclassroom.web;

import com.guigu.ababa.ababaclassroom.entity.Ab_user;
import com.guigu.ababa.ababaclassroom.entity.Ab_user_complete;
import com.guigu.ababa.ababaclassroom.entity.Denglu;
import com.guigu.ababa.ababaclassroom.util.CookieUtil;
import com.guigu.ababa.ababaclassroom.util.RedisStore;
import com.guigu.ababa.ababaclassroom.service.Ab_userService;
import com.guigu.ababa.ababaclassroom.handler.MD5;
import com.guigu.ababa.ababaclassroom.service.Ab_user_completeService;
import com.guigu.ababa.ababaclassroom.service.DuanxinService;
import com.guigu.ababa.ababaclassroom.util.StoreFactory;
import com.guigu.ababa.ababaclassroom.util.StoreInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Controller
@CrossOrigin
public class AbaCollertion {

    private static final Logger LOGGER = LoggerFactory.getLogger(AbaCollertion.class);

    //引入Ab_userService接口
    @Autowired
    private Ab_userService ab_userService;

    //引入Ab_user_completeService接口
    @Autowired
    private Ab_user_completeService ab_user_completeService;

    //引入DuanxinService接口
    @Autowired
    private DuanxinService duanxinService;

    //MD5接口
    @Autowired
    private MD5 md5;

    //Redis接口
    @Autowired
    private StoreInterface redisStore;

    //Test单点登陆
    private static final String USER_KEY="user_key";

    private Ab_user ab_user222;
    //发送短信验证码
    @GetMapping("/fsPhone/{phone}")
    @ResponseBody
    public String fsPhone(@PathVariable String phone){
        System.out.println(phone);
        return duanxinService.Phone(phone);
    }

    //账号密码登陆user用户
    @RequestMapping("/Loginuser")
    @ResponseBody
    public Object Loginuser(@RequestBody Ab_user ab_user,HttpServletResponse response,HttpServletRequest request){
        String username = ab_user.getUsername();
        String password = ab_user.getSalt();
        System.out.println(username);
        System.out.println(password);

        Ab_user ab_user1= new Ab_user();
        ab_user1.setSalt(password);
        ab_user1.setUsername(username);
        Ab_user selectlogin = ab_userService.Selectlogin(ab_user1);
        if (selectlogin==null){
            return "1";
        }
        request.getRequestDispatcher("/login?username="+username+"&password="+password+"");
        return selectlogin.getUsername();
    }

    //电话登陆user用户
    @RequestMapping("/Loginphone")
    @ResponseBody
    public String selectByone(@RequestBody Ab_user ab_user){
        System.out.println(ab_user.getCode());
        if (duanxinService.verify(ab_user.getCode())){
            System.out.println("进来啦");
            if (ab_user.getState()==0){
                System.out.println("进来啦1");
                ab_user222= ab_userService.Selectphonelogin(ab_user.getPhone());
                System.out.println(ab_user222);
                if (ab_user222==null){
                    return "1";  //封禁返回一
                }
                return ab_user222.getUsername();  //返回用户name
            }
        }
        return "0";//验证码或手机号未注册
    }

    //添加user用户
    @RequestMapping("/adduser")
    @ResponseBody
    public String insertuser(@RequestBody Ab_user ab_user){
        /*返回0  短信验证码错误   返回1  注册成功*/
        if (duanxinService.verify(ab_user.getCode())){
            int radom = new Random().nextInt(999999);
            if (radom < 100000) {
                radom += 100000;
            }
            String s = md5.MD5(radom+"");
            ab_user.setSalt(s);
            ab_user.setOpenid(null);
            int insertuser = ab_userService.insertuser(ab_user);
            System.out.println(ab_user.getUsername());
            return ab_user.getUsername();
        }else {
            return "0";
        }

    }

    //查询user用户
    @GetMapping("/finduser")
    @ResponseBody
    public List<Ab_user> finduser(){
        List<Ab_user> selectlist = ab_userService.selectlist();
        return selectlist;
    }

    //删除user用户(假删除)
    @RequestMapping("/update_deleuser")
    @ResponseBody
    public int update_deleuser(@RequestBody Ab_user ab_user){
        ab_user.setState(1);
        int updateuser = ab_userService.update_deleuser(ab_user);
        return updateuser;
    }


    //修改user用户
    @RequestMapping("/updateuser")
    @ResponseBody
    public int update(@RequestBody Ab_user ab_user){
        String salt = ab_user.getSalt();
        Ab_user ab_user1 = new Ab_user();
        ab_user.setSalt(md5.MD5(salt));
        if (duanxinService.verify(ab_user.getCode())){
            int updateuser = ab_userService.updateuser(ab_user1);
            return updateuser;
        }
        return 0;
    }

    //添加user_complete用户详细信息
    @PostMapping("/addcomplete")
    @ResponseBody
    public Ab_user_complete addusercomplete(@RequestBody Ab_user_complete ab_user_complete,HttpServletRequest request,HttpServletResponse response) {
        try {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Ab_user ab_user = ab_userService.SelectoneName(ab_user_complete.getAb_username());
        System.out.println(ab_user_complete.getAb_username());
        int userid = ab_user.getUserid();
        ab_user_complete.setUserid(userid);
        ab_user_complete.setGrade(0);
        ab_user_complete.setState(0);
        int i = ab_user_completeService.insertuser_complete(ab_user_complete);
        //System.out.println(ab_user_complete.getUserfullname());
        return ab_user_complete;
    }

    //查询单个
    @GetMapping("/findComplete/{name}")
    @ResponseBody
    public Ab_user_complete findComplete(@PathVariable("name") String name,HttpServletResponse response){
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        Ab_user ab_user = ab_userService.SelectoneName(name);
        int userid = ab_user.getUserid();
        System.out.println(userid);
        Ab_user_complete ab_user_complete = ab_user_completeService.selectuser_complete(userid);
        System.out.println(ab_user_complete);
        return ab_user_complete;
    }

    //查询user_complete用户详细信息
    @GetMapping("/findcomplete")
    @ResponseBody
    public List<Ab_user_complete> selectusercomplete(){
        return ab_user_completeService.findusercomplete();
    }

    //修改user_complete用户详细信息
    @RequestMapping("/updatecomplete")
    @ResponseBody
    public int updateusercomplete(@RequestBody Ab_user_complete ab_user_complete){
        return ab_user_completeService.updateusercomplete(ab_user_complete);
    }

    //修改user用户详细信息状态和user用户状态
    @RequestMapping("/updateuserid/{id}/{state}")
    @ResponseBody
    public int updateusercomid(@PathVariable("id") int id,@PathVariable("state") int state){
        ab_user_completeService.updatecompleteID(id,state);
        return ab_user_completeService.updateuserID(id,state);
    }

    //redis测试单点登陆
    @RequestMapping("/login")
    public void login(String username, HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> map = new HashMap<>();
        map.put("username",username);
        map.put("password",2222);
        System.out.println(username);

        redisStore.set(CookieUtil.getLoginCookie(request,response,true),map);
    }

    //redis测试单点登陆
    @RequestMapping("/getUser")
    public void getUser(HttpServletRequest request,HttpServletResponse response){
        Object user =request.getSession().getAttribute(USER_KEY);
        Object o =(Denglu)redisStore.get(CookieUtil.getLoginCookie(request, response, false));
        System.out.println(((Denglu) o).getUsername());
        System.out.println(o);
    }
}
